using System;
using System.Collections.Generic;

namespace shfa.snomtastic.data.business
{
    public class SubcategoryComparer : IComparer<SubcategoryInfo>
    {
        private string _sortExpression;
        public string SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

		private string _sortDirection;
        public string SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }

        public SubcategoryComparer(string sortExpression)
        {
            int iSplitPos = sortExpression.IndexOf(" ");
            SortExpression = (iSplitPos > 0) ? sortExpression.Substring(0, iSplitPos) : sortExpression;
            SortDirection = (iSplitPos > 0) ? sortExpression.Substring(iSplitPos + 1).ToUpper() : "ASC";
        }

        public SubcategoryComparer(string sortExpression, string sortDirection)
        {
            this.SortExpression = sortExpression;
            this.SortDirection = sortDirection;
        }

        int IComparer<SubcategoryInfo>.Compare(SubcategoryInfo x, SubcategoryInfo y)
        {
            SubcategoryInfo oX = (SortDirection == "ASC") ? x : y;
            SubcategoryInfo oY = (SortDirection == "ASC") ? y : x;

            switch (SortExpression)
            {				
				case "SubcategoryId":
					return oX.SubcategoryId.CompareTo(oY.SubcategoryId);				
				case "CategoryId":
					return oX.CategoryId.CompareTo(oY.CategoryId);				
				case "Name":
					return oX.Name.CompareTo(oY.Name);
				default:
                    return 0;
            }
        }
    }
}
